package com.study.demo.data.local;


import com.study.demo.data.ContentDataSource;
import com.study.demo.db.ThreadInfo;
import com.study.demo.db.ThreadReply;
import com.study.demo.greendao.ThreadInfoDao;
import com.study.demo.greendao.ThreadReplyDao;
import com.study.demo.util.HtmlUtil;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Single;
import io.reactivex.SingleEmitter;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by sll on 2016/6/3.
 */
public class ContentLocalDataSource implements ContentDataSource {

    private final ThreadInfoDao mThreadInfoDao;
    private final ThreadReplyDao mThreadReplyDao;

    @Inject
    public ContentLocalDataSource(ThreadInfoDao mThreadInfoDao, ThreadReplyDao mThreadReplyDao) {
        this.mThreadInfoDao = mThreadInfoDao;
        this.mThreadReplyDao = mThreadReplyDao;
    }

    @Override
    public Single<ThreadInfo> getThreadInfo(String fid, final String tid) {
        return Single.create((SingleEmitter<ThreadInfo> singleEmitter) -> {
                    List<ThreadInfo> threadInfos =
                            mThreadInfoDao.queryBuilder().where(ThreadInfoDao.Properties.Tid.eq(tid)).list();
                    if (!threadInfos.isEmpty()) {
                        ThreadInfo threadInfo = threadInfos.get(0);
                        String content = threadInfo.getContent();
                        threadInfo.setContent(HtmlUtil.transImgToLocal(content));
                        singleEmitter.onSuccess(threadInfo);
                    } else {
                        singleEmitter.onSuccess(null);
                    }
                }
        ).subscribeOn(Schedulers.io());
    }

    public void saveThreadInfo(ThreadInfo mThreadInfo) {
        mThreadInfoDao.queryBuilder()
                .where(ThreadInfoDao.Properties.Tid.eq(mThreadInfo.getTid()))
                .buildDelete()
                .executeDeleteWithoutDetachingEntities();
        mThreadInfoDao.insert(mThreadInfo);
    }


    @Override
    public Single<List<ThreadReply>> getReplies(String fid, final String tid, final int page) {
        return Single.create((SingleEmitter<List<ThreadReply>> singleEmitter) ->
                onSubscribe(singleEmitter, tid, page)
        ).subscribeOn(Schedulers.io());
    }

    private void onSubscribe(SingleEmitter<? super List<ThreadReply>> singleEmitter,
                                                   final String tid, final int page) {
        List<ThreadReply> replies = mThreadReplyDao.queryBuilder()
                .where(ThreadReplyDao.Properties.Tid.eq(tid),
                        ThreadReplyDao.Properties.IsLight.eq(false),
                        ThreadReplyDao.Properties.PageIndex.eq(page))
                .orderAsc(ThreadReplyDao.Properties.Floor)
                .list();
        if (replies.isEmpty() && page > 1) {
            singleEmitter.onSuccess(null);
        } else {
            singleEmitter.onSuccess(replies);
        }
    }

    @Override
    public Single<List<ThreadReply>> getLightReplies(String fid, final String tid) {
        return Single.create((SingleEmitter<List<ThreadReply>> singleEmitter) -> {
                    List<ThreadReply> replies = mThreadReplyDao.queryBuilder()
                            .where(ThreadReplyDao.Properties.Tid.eq(tid),
                                    ThreadReplyDao.Properties.IsLight.eq(true))
                            .orderDesc(ThreadReplyDao.Properties.Light_count)
                            .list();
                    singleEmitter.onSuccess(replies);
                }
        ).subscribeOn(Schedulers.io());
    }

    public void saveThreadReply(ThreadReply reply) {
        mThreadReplyDao.queryBuilder()
                .where(ThreadReplyDao.Properties.Pid.eq(reply.getPid()),
                        ThreadReplyDao.Properties.IsLight.eq(reply.getIsLight()))
                .buildDelete()
                .executeDeleteWithoutDetachingEntities();
        mThreadReplyDao.insert(reply);
    }
}
